package kitwee.com.kanpad.bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kitwee.com.kanpad.R;


/**
 * Created by sven on 2017/11/25.
 */

public class DepartmentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<DepartmentInfo.DataBean.RowsBean> departList;
    private Context context;

    public DepartmentListAdapter(List<DepartmentInfo.DataBean.RowsBean> departList, Context context) {
        this.departList = departList;
        this.context = context;
    }

    public void setData(List<DepartmentInfo.DataBean.RowsBean> depart){
        departList=depart;
    }
    public DepartmentListAdapter() {
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_department, parent, false);
        DepartHolder holder = new DepartHolder(view);
        return holder;
    }

    private int layoutPosition;
    private boolean select;

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof DepartHolder ){
            ((DepartHolder)holder).mTextView.setText("" + departList.get(position).getName());
            ((DepartHolder)holder).itemView.setTag(position);
            if (mOnItemClickLitener != null) {
                ((DepartHolder)holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        select = true;
                        layoutPosition = holder.getLayoutPosition();
                        notifyDataSetChanged();
                        mOnItemClickLitener.onItemClick(holder.itemView, (int) holder.itemView.getTag());
                    }
                });
            }

            if (position == layoutPosition && select) {
                ((DepartHolder)holder).itemView.setBackgroundColor(context.getResources().getColor(R.color.itemclick));
            } else {
                ((DepartHolder)holder).itemView.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        }


    }

    @Override
    public int getItemCount() {
        return departList.size();
    }

    public static class DepartHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text)
        TextView mTextView;
        public DepartHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
